package com.escool.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.escool.model.TbAlumno;
@Repository
public class AlumnoDAOImpl implements AlumnoDAO {
	
	@Autowired
	SessionFactory sessionFactory;

	@Override
	public void add(TbAlumno alumno) {
		sessionFactory.getCurrentSession().save(alumno);

	}

	@Override
	public void edit(TbAlumno alumno) {
		sessionFactory.getCurrentSession().update(alumno);

	}

	@Override
	public void delete(Integer id) {
		TbAlumno tbAlumno = (TbAlumno) sessionFactory.getCurrentSession().load(TbAlumno.class, id);
		if(tbAlumno != null){
			sessionFactory.getCurrentSession().delete(tbAlumno);
		}

	}

	@Override
	public List<TbAlumno> listAll() {
		
		return sessionFactory.getCurrentSession().createQuery("from TbAlumno").list();
	}

	@Override
	public TbAlumno find(Integer id) {
		
		return (TbAlumno) sessionFactory.getCurrentSession().get(TbAlumno.class, id);
	}

	@Override
	public List<TbAlumno> listByResponsable(Integer id) {
		String hql = "from TbAlumno where tbResponsable.idResponsable =" + id;
		return sessionFactory.getCurrentSession().createQuery(hql).list();
	}
	
	@Override
	public List<TbAlumno> listByCodigo(String cod) {
		String hql = "from TbAlumno where codigoAlumno ='" + cod + "'";
		return sessionFactory.getCurrentSession().createQuery(hql).list();
	}

}
